package com.book.redis.impl;

import com.book.redis.RedisRandomProvider;
import com.book.util.Constants;
import com.book.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author 李锦程
 * @Date 2021/4/5 4:35 下午
 * @Package com.book.redis.impl
 * @Version 1.0
 */
@Component
public class RandomProviderImpl implements RedisRandomProvider {
    private static final String INDEX_USER = "YSWU";
    private static final String BOOK_CODE = "YSWB";
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public String getCode() {
        String s = redisUtil.get(Constants.USER_CODE);
        int code = Integer.parseInt(s)+1;
        redisUtil.set(Constants.USER_CODE, Integer.toString(code));
        return INDEX_USER.concat(s);
    }

    @Override
    public String getBookCode() {
        String s = redisUtil.get(Constants.BOOK_KEY);
        int code = Integer.parseInt(s)+1;
        redisUtil.set(Constants.BOOK_KEY, Integer.toString(code));
        return BOOK_CODE.concat(s);
    }

    @Override
    public String getImgNum() {
        String s = redisUtil.get(Constants.IMG_NUM);
        int code = Integer.parseInt(s)+1;
        redisUtil.set(Constants.IMG_NUM, Integer.toString(code));
        return Integer.toString(code);
    }

    @Override
    public String getNo() {
        String s = redisUtil.get("no");
        int code = Integer.parseInt(s)+1;
        redisUtil.set("no", Integer.toString(code));
        return Integer.toString(code);
    }
}
